﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ECare.BUS;
using ECare.Client.templates;

namespace ECare.Client.frms
{
    public partial class frmUserInfo : frmInfo
    {
        public frmUserInfo()
        {
            InitializeComponent();

            this.Load += frmUserInfo_Load;
        }
        //******************************************************************
        //Author: wxw
        //Date: 2014/09/04
        public void SetNameProperty()
        {
            this.txtLoginName.ReadOnly = true;
        }void frmUserInfo_Load(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            //Load the users.
            this.LoadRoles();

            if (this.entity != null)
            {
                this.txtName.Text = (this.entity as UserInfo).UserName;
                this.txtPassword.Text = (this.entity as UserInfo).UserPassword;
                this.txtLoginName.Text = (this.entity as UserInfo).UserLoginName;
                this.cbRoles.SelectedValue = (this.entity as UserInfo).RoleId;
                this.txtemail.Text = (this.entity as UserInfo).UserEmail;
                this.txtPhone.Text = (this.entity as UserInfo).UserPhone;
                //是否激活
                this.cbActive.Checked = (this.entity as UserInfo).IsActive;
            }
        }

        private void LoadRoles()
        {
            var roles = RoleInfoHelper.GetRoles();
            this.cbRoles.DataSource = roles;
            this.cbRoles.DisplayMember = "RoleName";
            this.cbRoles.ValueMember = "RoleId";
        }
        public event EntitySavedHandler Saved;
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtName.Text))
            {
                this.errorProvider1.SetError(this.txtName, "empty.");
                return;
            }
            else
            {
                this.errorProvider1.SetError(this.txtName, string.Empty);
            }


            if (string.IsNullOrEmpty(this.txtLoginName.Text))
            {
                this.errorProvider1.SetError(this.txtLoginName, "empty");
                return;
            }
            else
            {
                this.errorProvider1.SetError(this.txtLoginName, "");
            }

            if (string.IsNullOrEmpty(this.txtPassword.Text))
            {
                this.errorProvider1.SetError(this.txtPassword, "empty");
                return;
            }
            else
            {
                this.errorProvider1.SetError(this.txtPassword, "");
            }


            if (this.cbRoles.SelectedValue==null)
            {
                     this.errorProvider1.SetError(this.cbRoles, "empty");
                return;
            }
            else{
                   this.errorProvider1.SetError(this.cbRoles, "");
        }

            if (this.entity == null)
            {
                var newuser = new UserInfo
                {
                    UserLoginName = this.txtLoginName.Text,
                    UserEmail = this.txtemail.Text,
                    UserName = this.txtName.Text,
                    UserPassword = this.txtPassword.Text,
                    UserPhone = this.txtPhone.Text,
                    RoleId =int.Parse(this.cbRoles.SelectedValue.ToString())

                };
                var userid = UserInfoHelper.CreateUserInfo(newuser);
                newuser.UserId = userid;
                this.entity = newuser;
            }
            else
            {
                var edituser = this.entity as UserInfo;
                edituser.UserLoginName = this.txtLoginName.Text;
                edituser.UserEmail = this.txtemail.Text;
                edituser.UserName = this.txtName.Text;
                edituser.UserPassword = this.txtPassword.Text;
                edituser.UserPhone = this.txtPhone.Text;
                edituser.RoleId = int.Parse(this.cbRoles.SelectedValue.ToString());

                UserInfoHelper.UpdateUserInfo(edituser);
            }
            if (this.Saved != null)
            {
                this.Saved(this.entity);
                this.Close();
            }
        }
        //*************************************************************************************
        //Author: wxw
        //Date: 2014/09/04
        private void txtName_Leave(object sender, EventArgs e)
        {
            bool flag;

            if (this.entity == null)
            {
                var newuser = new UserInfo
                {
                    UserName = this.txtName.Text,
                };
                flag = UserInfoHelper.CheckUserLoginName(newuser);
            }
            else
            {
                var edituser = this.entity as UserInfo;
                edituser.UserName = this.txtName.Text;
                flag = UserInfoHelper.CheckUserLoginName(edituser);
            }
            if (!flag)
            {
                this.errorProvider1.SetError(this.txtName, "The user name has existed.");
            }
            else
            {
                this.errorProvider1.Clear();
            }
        }

        private void txtLoginName_Leave(object sender, EventArgs e)
        {
            bool flag;
            if (this.txtLoginName.ReadOnly == false)
            {

                if (this.entity == null)
                {
                    var newuser = new UserInfo
                    {
                        UserLoginName = this.txtLoginName.Text,
                    };
                    flag = UserInfoHelper.CheckUserLoginName(newuser);
                }
                else
                {
                    var edituser = this.entity as UserInfo;
                    edituser.UserLoginName = this.txtLoginName.Text;
                    flag = UserInfoHelper.CheckUserLoginName(edituser);
                }
                if (!flag)
                {
                    this.errorProvider1.SetError(this.txtLoginName, "The user login name has existed.");
                }
                else
                {
                    this.errorProvider1.Clear();
                }
            }
        }
    }
}
